<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Info;
use App\upload;
class TestController extends Controller
{
    //


    public function index(){

    	return view('test.index');
    }


    public function store(Request $request)
	{
//对提交过来的字段进行验证
	    $this->validate($request, [
	        'name' => 'required',
	        'machine' => 'required',
	        'code' => 'required',
	        'photonumber' => 'required',
	        'address' => 'required',
	        'describe' => 'required',
	    ]);
	   
	    
	    $time=date('Y-m-d-h-i-s');    
//将数据放进模型中
	    $info = new Info;
	    $info->name = $request->get('name');
	    $info->machine = $request->get('machine');
	    $info->code = $request->get('code');
	    $info->photonumber = $request->get('photonumber');
	    $info->address = $request->get('address');
	    $info->describe = $request->get('describe');
 //判断是否有上传图片
	   if (! empty ( $_FILES ['pic'] ['name'] )) {
            $tmp_file = $_FILES ['pic'] ['tmp_name'];
            $file_types = explode ( ".", $_FILES ['pic'] ['name'] );
            $file_type = $file_types [count ( $file_types ) - 1];

            //判断是不是照片格式
            if (!(strtolower ( $file_type ) == "png"||strtolower ( $file_type ) == "jpeg"||strtolower ( $file_type ) == "jpg"||strtolower ( $file_type ) == "gif"))              
            {
                return redirect()->back()->with('errors','不是照片文件,请重新上传');
            }

            /*设置上传路径*/
            $savePath =rtrim(app_path(),'/app').'public\upload\\';

            /*以时间来命名上传的文件*/
            $file_name = $time . "." . $file_type;

            /*是否上传成功*/
            if (!move_uploaded_file( $tmp_file, $savePath.$file_name )) {
                return redirect()->back()->with('errors','上传失败');
            }
            $info->pic = $savePath.$file_name;
        }
//邮件发送的相关信息
	    	$email='415873846@qq.com';
	    	$name='售后维修';
	    	$uid='25';
	    	$code='abc';
	    	
	    	
	    	//名字加密后与数字组成excel的文件名
	    	//$filename=md5($info->name).rand(1,1000);

	    	$subject='('.$info->name.')'.'的报修信息+('.$time.')';
	    	

//邮件发送之前先导出为execel文件,文件默认保存到 storage/exports 目录下.
			$cellData = [
			       ['','',$subject],
			       ['报修日期','客户名称','机器型号','机器条码','报修人','联系人电话','联系人地址','不良现象描述'],
			       [$time,$info->name,$info->machine,$info->code,$info->name,$info->photonumber,$info->address,$info->describe],
			     
			    ];
			//导出excel ，指定文件名
		    \Excel::create(iconv('UTF-8', 'GBK', $time ), function($excel) use ($cellData){
		     $excel->sheet('售后维修', function($sheet) use ($cellData){
		         $sheet->rows($cellData);
		     });
			})->store('xls',public_path('excel'));

//邮件发送 $data是传递到视图的数据
			// \Mail::send('activemail', $data, function($message) use($data)
			// {
			//     $message->to($data['email'], $data['name'])->subject($data['subject']);
			//     // "exports\\".$data['subject'].'.xls'
			//     $attachment = storage_path("exports\\".$data['filename'].".xls");//只适用于英文名字和路径
  	// 			//在邮件中上传附件
  	// 			$message->attach($attachment,['as'=>$data['subject'].".xls"]);
			// });
			
			$url = "service.com/excel/".$time.".xls";
			$data = ['email'=>$email, 'name'=>$name, 'uid'=>$uid, 'filename'=>$time,'url'=>$url ,'activationcode'=>$code,'subject'=>$subject];
		    \Mail::send('activemail',$data,function($message) use ($data){
		    		$message->to($data['email'],$data['name'])->subject($data['subject']);
		    });
//存进数据库，并返回信息填写页面	
	    if ($info->save()) {
	        return redirect()->back()->withInput()->withErrors('提交成功!');
	    } else {
	        return redirect()->back()->withInput()->withErrors('保存失败！');
	    }
	}



}
